package com.controller;

import com.entity.*;
import com.service.OrderService;
import com.service.OutGoodsService;
import com.service.UnloadGoodsService;
import com.vo.OrderClientVO;
import com.vo.OrderRefundVO;
import com.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private UnloadGoodsService unloadGoodsService;

    @Autowired
    private OutGoodsService outGoodsService;

    //查询所以订单
    @RequestMapping("/getAll/{pageIndex}/{pageSize}")
    public Result getAll(
          @PathVariable("pageIndex") Integer pageIndex,
          @PathVariable("pageSize") Integer pageSize,
          @RequestBody OrderVO orderVO
    ){
        System.out.println(orderVO);
        PageBean<Order> pageBean = orderService.getAll(pageIndex, pageSize, orderVO);
        return Result.ok(pageBean);
    }

    //添加订单
    @RequestMapping("/add")
    public Result add(@Valid @RequestBody OrderClientVO orderClientVO, BindingResult bindingResult){
        Map<String, String> map = new HashMap<>();
        if(bindingResult.getErrorCount() > 0){
            //获取所有错误字段信息的集合
            bindingResult.getFieldErrors().forEach(item -> {
                map.put(item.getField(), item.getDefaultMessage());
            });
        }
        //判断map集合中是否有数据，有数据说明数据有问题
        if(map.size() > 0){
            return new Result(100, "error", map);
        }else {
            System.out.println("orderclientvo="+orderClientVO);

            Employee employee = orderService.getByCname(orderClientVO.getCreation());
            GoodsEntity goodsEntity = orderService.getByGname(orderClientVO.getGname());

            OutGoods outGoods = new OutGoods();
            outGoods.setOname(goodsEntity.getGname());
            outGoods.setOutNumber(orderClientVO.getNumber());
            outGoods.setGoodsUniqueNo(goodsEntity.getGoodsUniqueNo());
            outGoods.setGid(goodsEntity.getGid());
            outGoods.setEid(employee.getEid());


            System.out.println(outGoods);

            outGoodsService.outGoods(outGoods);
            orderService.add(orderClientVO);
            return Result.ok();
        }
    }

    //删除订单
    @RequestMapping("/remove")
    public Result remove(Integer oid){
        int[] oids = {oid};
        orderService.remove(oids);
        return Result.ok();
    }

    //批量删除订单
    @RequestMapping("/removeBatch")
    public Result removeBatch(@RequestBody int[] oids){
        orderService.remove(oids);
        return Result.ok();
    }

    //修改订单
    @RequestMapping("/modify")
    public Result modify(@RequestBody Order order){
        orderService.modify(order);
        return Result.ok();
    }

    //获取订单号
    @RequestMapping("/getOrderNumber")
    public Result getOrderNumber(){
        return Result.ok(orderService.getOrderNumber());
    }

    //收货
    @RequestMapping("/delivery")
    public Result delivery(@RequestBody Order order){
        orderService.delivery(order);
        return Result.ok();
    }

    //退货
    @RequestMapping("/refund")
    public Result refund(@RequestBody OrderRefundVO orderRefundVO){
        System.out.println(orderRefundVO);
        UnloadGoods unloadGoods = new UnloadGoods();
        unloadGoods.setGname(orderRefundVO.getGoodsEntity().getGname());
        unloadGoods.setCurrentNumber(orderRefundVO.getNumber());
        unloadGoods.setDeliveryMan(orderRefundVO.getCreator());
        unloadGoods.setDeliveryNo(orderRefundVO.getOrdernumber());


        //获取第二天的时间
        Date date = new Date();
        Date toDay = new Date(date.getTime() + (24 * 60 * 60 * 1000));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        unloadGoods.setStorageTime(dateFormat.format(toDay));

        unloadGoods.setUnloadgoodsDesc("退库原因：" + orderRefundVO.getCause());
        unloadGoods.setSource("销售退货");


        unloadGoodsService.addUnloadGoodsIsSale(unloadGoods);
        orderService.refund(orderRefundVO);
        return Result.ok();
    }

    //统计
    @RequestMapping("/stat")
    public Result stat(String month){
        System.out.println(month);
        return Result.ok(orderService.stat(month));
    }

    //年报表
    @RequestMapping("/statYear")
    public Result statYear(String year, String creator){
        return Result.ok(orderService.statYear(year, creator));
    }
}
